package packageCommand;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

import packagecmdimp.CheckCommand;
import packagerole.User;

public class UserCommandFactory extends SystemCommandFactory {

	private static Map<CommandCode, Class<? extends Command>> commandMap = new HashMap<>();

	static {
		commandMap.put(CommandCode.CHECK, CheckCommand.class);
		// commandMap.put(CommandCode.ALTERORDER, AlterOrderCommand.class);
		// commandMap.put(CommandCode.ALTERGOODS, AlterGoodsCommand.class);
		// commandMap.put(CommandCode.SEND, SendCommand.class);
		// commandMap.put(CommandCode.CHECK, CheckCommand.class);
	}

	private User user;

	public UserCommandFactory(User user) {
		this.user = user;
	}

	@Override
	public Command buildCommand(CommandCode cmdCode) {
		Command command = null;

		Class<? extends Command> cmdClass = commandMap.get(cmdCode);

		if (cmdClass != null) {
			try {
				command = cmdClass.getConstructor(User.class).newInstance(user);
			} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
					| InvocationTargetException | NoSuchMethodException | SecurityException e) {
				e.printStackTrace();
			}
		}

		if (command == null) {
			command = super.buildCommand(cmdCode);
		}

		return command;
	}
}
